package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DyUserinfo;
import com.ruoyi.system.service.IDyUserinfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户卡信息Controller
 * 
 * @author ruoyi
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/system/dyUserinfo")
public class DyUserinfoController extends BaseController
{
    @Autowired
    private IDyUserinfoService dyUserinfoService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户卡信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:dyUserinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(DyUserinfo dyUserinfo)
    {
        startPage();
        List<DyUserinfo> list = dyUserinfoService.selectDyUserinfoList(dyUserinfo);

        SysUser user = sysUserService.selectUserById(this.getUserId());
//        System.out.println(user);
        if(user.getIsShowItemName()==0 && user.getUserId()!=1){
            for(DyUserinfo info: list){
                info.setItemName(null);
            }
        }

        return getDataTable(list);
    }

    @GetMapping("/over/{ids}")
    public AjaxResult over(@PathVariable Long[] ids)
    {
        return toAjax(dyUserinfoService.overDyUserinfoByIds(ids));
    }



    /**
     * 导出用户卡信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:dyUserinfo:export')")
    @Log(title = "用户卡信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DyUserinfo dyUserinfo)
    {
        List<Long> ids = dyUserinfo.getIds();
        List<DyUserinfo> list = new ArrayList<>();
        if(ids!=null && ids.size()>0){
            list = dyUserinfoService.selectDyUserinfoListByIds(ids);
        }else{
            list = dyUserinfoService.selectDyUserinfoList(dyUserinfo);
        }
//        System.out.println(list);
        if(list!=null){
            for(DyUserinfo item: list){
                dyUserinfoService.updateExp(item.getId());
            }
        }
        ExcelUtil<DyUserinfo> util = new ExcelUtil<DyUserinfo>(DyUserinfo.class);
        util.exportExcel(response, list, "用户卡信息数据");
    }

    /**
     * 获取用户卡信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dyUserinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        DyUserinfo info = dyUserinfoService.selectDyUserinfoById(id);
        SysUser user = sysUserService.selectUserById(this.getUserId());
        if(user.getIsShowItemName()==0 && user.getUserId()!=1){
            info.setItemName(null);
        }
        return success(info);
    }

    /**
     * 新增用户卡信息
     */
    @PreAuthorize("@ss.hasPermi('system:dyUserinfo:add')")
    @Log(title = "用户卡信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DyUserinfo dyUserinfo)
    {
        return toAjax(dyUserinfoService.insertDyUserinfo(dyUserinfo));
    }

    /**
     * 修改用户卡信息
     */
    @PreAuthorize("@ss.hasPermi('system:dyUserinfo:edit')")
    @Log(title = "用户卡信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DyUserinfo dyUserinfo)
    {
        return toAjax(dyUserinfoService.updateDyUserinfo(dyUserinfo));
    }

    /**
     * 删除用户卡信息
     */
    @PreAuthorize("@ss.hasPermi('system:dyUserinfo:remove')")
    @Log(title = "用户卡信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return toAjax(dyUserinfoService.deleteDyUserinfoByIds(ids));
    }
}
